package com.example.hello.controller;

import com.example.hello.pojo.Res;
import com.example.hello.service.RatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rating")
public class RatingController {

    @Autowired
    private RatingService ratingService;

    /**
     * GET /rating?userId=1&bookId=101
     * 查询用户对该书的评分，查到返回评分，查不到返回 0.
     */
    @GetMapping
    public Res getRating(@RequestParam Integer userId, @RequestParam Integer bookId) {
        // Service 层返回原始的评分数据
        Double rating = ratingService.getRating(userId, bookId);
        // Controller 层使用 Res.success() 封装返回结果
        return Res.success(rating);
    }
}